import { SET_ADDRESS, CHANGE_ADDRESS, ADD_ADDRESS } from '../../actions/index'

// 定义初始数据
let initState = {
    curAddress: '',
    addressList: []
}


export default function (state = initState, action) {
    // 深拷贝
    let newState = JSON.parse(JSON.stringify(state))

    switch(action.type) {
        case SET_ADDRESS:
            // 设置地址列表
            newState.addressList = action.address
            return newState
            // return { ...state, addressList: action.address }
        case CHANGE_ADDRESS:
            // 修改选中状态
            newState.addressList.forEach(item => {
                if (item.id === action.id) {
                    item.flag = true
                } else {
                    item.flag = false
                }
            })
            // 修改当前地址
            newState.curAddress = action.address
            return newState
        case ADD_ADDRESS:
            // 新增地址
            newState.addressList.push(action.options)
            return newState
        default : 
            return state
    }
}